<?php
class DefaultController extends TestController 
{
	//权限控制
	public function accessRules()
	{
		$accessRules = parent::accessRules();
		$allowRules = array(
				array('allow',
						'actions'=>array('index','main'),
						'users'=>array('@')
				),
				array('allow',
						'actions'=>array('login','logout','error'),
						'users'=>array('*')
				),
		);
		return CMap::mergeArray($allowRules, $accessRules);
	}
	/**
	 * 后台首页
	 *
	 */
    public function actionIndex() {
        //$this->layout='empty';
        $menu = AdminMenu::getMenu(0,1);
        $leftmenu = $this->getLeftMenu($menu);
		$this->render('index', array('menu'=>$menu ,'leftmenu'=>$leftmenu));
	}
	
    public function getLeftMenu($menu){
    	$role_auth = Yii::app()->user->get_auth_power();
    	$role_auth_keys = array_keys($role_auth);
    	$aReturn='';
    	foreach ($menu as $k=>$v){
    		if (isset($v['child']) && !empty($v['child'])){
    			$aChild = $this->getChildMenu($v['child']);
    		}else{
    			$aChild = '';
    		}
    		if(in_array(Yii::app()->user->id,Yii::app()->params['administrator']) || in_array($v['menu_id'], $role_auth_keys)){
    		$aReturn[]=array(
	    				'id'=>($k+1),
	    				'menu'=>$aChild
	    				);
    		}
	    }
	    return $aReturn;
    }
    public function getChildMenu($nextMenu){
    	if(empty($nextMenu)) return false;
    	$role_auth = Yii::app()->user->get_auth_power();
    	$role_auth_keys = array_keys($role_auth);
    	$aReturn='';
    	foreach ($nextMenu as $v){
    		$aChild = array();
    		if (isset($v['child']) && !empty($v['child'])){
    			foreach ($v['child'] as $cv){
    				if(in_array(Yii::app()->user->id,Yii::app()->params['administrator']) || in_array($cv['menu_id'], $role_auth_keys)){
	    				$aChild[] = array(
	    						'id'=>$cv['menu_id'],
	    						'text'=>$cv['menu_name'],
	    						//'tpl'=>'<a href="{href}" target="_blank"><em>{text}</em></a>',
	    						'href'=>$this->createUrl($cv['controller_id'].'/'.$cv['action_id'])
	    				);
    				}
    			}
    		}
    		if(in_array(Yii::app()->user->id,Yii::app()->params['administrator']) || in_array($v['menu_id'], $role_auth_keys)){
	    		$aReturn[]=array(
	    			'text'=>$v['menu_name'],
	    			'collapsed'=>false,
	    			'items'=>$aChild
	    		);
    		}
    	}
    	return $aReturn;
    }
    
	//首页默认
	public function actionMain()
	{
		
		$this->render('main');
	}
	//登录
	public function actionLogin()
	{		
		$this->layout='/layouts/empty';
		$model=new AdminLoginForm('login');
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
        
		if(isset($_POST['submitLogin'])){
			$model->rememberMe=isset($_POST['rememberMe'])?$_POST['rememberMe']:'';
			$model->username=$_POST['username'];
			$model->password=$_POST['password'];
			$model->verifyCode=$_POST['verifyCode'];
			if($model->validate() && $model->login()){
				$this->redirect($this->createUrl('default/index'));
			}else{
				Yii::app()->user->setFlash('flash',CHtml::errorSummary($model));
			}
		}
		$this->render('login');
	}
	//退出
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect($this->createUrl('default/login'));
	}
	
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}
}